package com.example.user.myhjjf_app.presenter;

import android.app.Activity;
import android.graphics.Color;
import android.util.Log;

import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.PolygonOptions;
import com.baidu.mapapi.map.Stroke;
import com.baidu.mapapi.map.TextOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;
import com.baidu.mapapi.search.district.DistrictResult;
import com.example.user.myhjjf_app.model.Model_Area;
import com.example.user.myhjjf_app.view.Activity_District;
import com.example.user.myhjjf_app.view.Activity_Login;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by User on 2017/8/16.
 */

public class Presenter_District{
    Model_Area model_area;
    List<String> testName = new ArrayList<>();
    List<String> Colors = new ArrayList<>();
    List<List<LatLng>> lists;
    List<String> id = new ArrayList<>();
    public Presenter_District(Model_Area model_area){
        this.model_area = model_area;
        intent();
    }

    public void intent(){
        lists = new ArrayList<>();
        for (int i = 0; i <model_area.getValue().getArray().size(); i++) {
            Colors.add(model_area.getValue().getArray().get(i).getColor());
            testName.add(model_area.getValue().getArray().get(i).getName());
            id.add(model_area.getValue().getArray().get(i).getId());
            List<LatLng> latLngs = new ArrayList<>();
            for (int j = 0; j < model_area.getValue().getArray().get(i).getBoundary().size(); j++) {
                double lng = model_area.getValue().getArray().get(i).getBoundary().get(j).getLng();
                double lat = model_area.getValue().getArray().get(i).getBoundary().get(j).getLat();
                latLngs.add(new LatLng(lat,lng));
            }
            lists.add(latLngs);
        }
        Activity_District.id = id;
        Activity_District.lists = lists;
    }

    public void setMap() {
        LatLngBounds.Builder builder = new LatLngBounds.Builder();
        for (int i = 0; i < lists.size(); i++) {
            List<LatLng> latLngs = lists.get(i);
            OverlayOptions  ooplygn = new PolygonOptions().points(latLngs).stroke(new Stroke(5, android.graphics.Color.BLACK)).fillColor(Color.parseColor(colo(Colors.get(i))));
            Activity_District.baiduMap.addOverlay(ooplygn);
            for (LatLng latLng : latLngs) {
                builder.include(latLng);
            }
            //LatLng lat = builder.build().getCenter();
            LatLng lat = new LatLng(model_area.getValue().getArray().get(i).getLat(),model_area.getValue().getArray().get(i).getLng());
            OverlayOptions ooplygn2 = new TextOptions().text(testName.get(i)).fontSize(40).fontColor(0xFF000000).position(lat);
            Activity_District.baiduMap.addOverlay(ooplygn2);
        }

        Activity_District.baiduMap.setMapStatus(MapStatusUpdateFactory.newLatLngBounds(builder.build()));
        MapStatusUpdate u = MapStatusUpdateFactory.zoomTo(14);
        Activity_District.baiduMap.animateMapStatus(u);
    }

    public String colo(String color){
        int pos = color.indexOf('#');
        StringBuffer stringBuffer = new StringBuffer(color);
        String co = stringBuffer.insert(pos+1,"4D").toString();
        return co;
    }

}
